<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class Massif extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_massif_list';



    protected $append  = [

        'service'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-12-30 14:18
     * @功能说明:服务类型
     */
    public function getServiceAttr($value,$data){

        if(!empty($data['id'])){

            $service_model = new MassifService();

            $list = $service_model->where(['massif_id'=>$data['id']])->select()->toArray();

            return $list;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(!empty($data['service'])){

            $service = $data['service'];

            unset($data['service']);
        }

        $res = $this->insert($data);

        $id = $this->getLastInsID();

        if(!empty($service)){

            $this->updateSome($id,$service,$data['uniacid']);

        }

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-20 16:38
     * @功能说明:
     */
    public function updateSome($id,$data,$uniacid){

        $service_model = new MassifService();

        $service_model->where(['massif_id'=>$id])->delete();

        if(!empty($data)){

            foreach ($data as $v){

                $v['massif_id'] = $id;

                $v['uniacid']   = $uniacid;

                $service_model->insert($v);

            }

        }

        return true;


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(!empty($data['service'])){

            $service = $data['service'];

            unset($data['service']);
        }

        $res = $this->where($dis)->update($data);

        if(!empty($service)){

            $this->updateSome($dis['id'],$service,$data['uniacid']);

        }

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}